//
//  Untitled.swift
//  PLDownload
//
//  Created by bin hu on 2025/7/7.
//

import CryptoSwift

/// AES解密处理器
class AESDecryptor {
    func decryptTS(data: Data, key: Data, iv: Data) throws -> Data {
        guard key.count == 16 else {
            throw DownloadError.invalidKeyLength
        }
        
        // AES-128 CBC模式解密
        let aes = try AES(key: key.bytes, blockMode: CBC(iv: iv.bytes), padding: .noPadding)
        let decryptedBytes = try aes.decrypt(data.bytes)
        return Data(decryptedBytes)
    }
    
    func validateDecryption(decryptedData: Data) -> Bool {
        // 检查TS文件头 (0x47)
        return decryptedData.count > 0 && decryptedData[0] == 0x47
    }
}

